/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var markers_array = [];
var map;
var currentIndex;
var initCenter = 0;
var nextCenter;
var latDelta, lngDelta;

/**
 * init a map
 */
function initialize_map() {
    clearMarkers();

    initCenter = new google.maps.LatLng(
        parseFloat(document.getElementById("lat").innerHTML),
        parseFloat(document.getElementById("lng").innerHTML));

    if (map == null) {
        var myOptions = {
            zoom: 13,
            center: initCenter,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.HYBRID,
            zoomControl: true,
            zoomControlOptions: {style: google.maps.ZoomControlStyle.SMALL}
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    } else {
        map.setCenter(initCenter);
    }
    
    loadMarkers();
}

/**
 * Load markers placed on each location
 */
function loadMarkers() {
    var research_id = document.getElementById("research_id").innerHTML;
    $.get("/data/getLocation/id/" + research_id, function(data) {
        var markers = jQuery.parseJSON(data);
        for (var i in markers) {
            var latlng = new google.maps.LatLng(
                parseFloat(markers[i].lat),
                parseFloat(markers[i].lng));
            var nextIdx = i < markers.length ? i : 0;
            var marker = new google.maps.Marker({
                icon: '/images/rbis_marker.png',
//                icon: 'http://google-maps-icons.googlecode.com/files/info.png',
                position: latlng,
                map: map,
                obsId: markers[i].id,
                next: new google.maps.LatLng(
                        parseFloat(markers[nextIdx].lat),
                        parseFloat(markers[nextIdx].lng))
            });
            if (initCenter.equals(latlng))
                currentIndex = i;
            bindMarker_onClick(marker);
            marker.attachTipTool(markers[i].location);
            
            markers_array.push(marker);
        }
    });
}

/**
 * On click a marker, load page for that location
 */
function bindMarker_onClick(marker) {
    google.maps.event.addListener(marker, 'click', function() {
        var obs_id = getParamValue(window.location.href, 'obs');
        var depth_id = getParamValue(window.location.href, 'depth');
        var new_url = window.location.href.replace("obs/"+obs_id, "obs/"+marker.obsId);
        new_url = new_url.replace("depth/"+depth_id, "");
        window.location = new_url;
    });
}

function clearMarkers() {
    if (markers_array) {
        for (i in markers_array) {
            markers_array[i].setMap(null);
        }
        markers_array.length = 0;
    }
}

/**
 * Center to next location
 */
function panToNext() {
    currentIndex = (currentIndex+1) % markers_array.length;
    nextCenter = markers_array[currentIndex].getPosition();
    var currentCenter = map.getCenter();
    latDelta = (nextCenter.lat() - currentCenter.lat()) / 10;
    lngDelta = (nextCenter.lng() - currentCenter.lng()) / 10;
    
    timerHandler();
}

function timerHandler() {
    if (Math.abs(map.getCenter().lat() - nextCenter.lat()) > Math.abs(latDelta)) {
        map.setCenter(new google.maps.LatLng(map.getCenter().lat() + latDelta, map.getCenter().lng() + lngDelta));
        setTimeout("timerHandler()", 100);
    } else {
        map.setCenter(new google.maps.LatLng(nextCenter.lat(), nextCenter.lng()));
        google.maps.event.trigger(markers_array[currentIndex], 'mouseover');
    }
}

/**
 * createElem() and $()
 * General helper functions for creating and accessing html elements.
 * @author Esa 2008
 */
function createElem(opt_className, opt_html, opt_tagName) {
  var tag = opt_tagName||"div";
  var elem = document.createElement(tag);
  if (opt_html) elem.innerHTML = opt_html;
  if (opt_className) elem.className = opt_className;
  return elem;
}
// another Esa's Little Helper
//function $(id_){
//  return document.getElementById(id_);
//}